package com.tianji.promotion.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponDiscountDTO;
import com.tianji.promotion.domain.dto.OrderCouponDTO;
import com.tianji.promotion.domain.dto.OrderCourseDTO;
import com.tianji.promotion.domain.query.CouponQuery;
import com.tianji.promotion.domain.query.UserCouponQuery;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.IUserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户领取优惠券的记录，是真正使用的优惠券信息 前端控制器
 * </p>
 *
 * @author mxl
 * @since 2024-07-01
 */
@Api(tags = "用户券相关接口")
@RestController
@RequestMapping("/user-coupons")
@RequiredArgsConstructor
public class UserCouponController {

    private final IUserCouponService userCouponService;

    @ApiOperation("领取优惠券")
    @PostMapping("{id}/receive")
    public void receiveCoupon(@PathVariable Long id){
        userCouponService.receiveCoupon(id);
    }

    @ApiOperation("根据兑换码领取优惠券")
    @PostMapping("{code}/exchange")
    public void exchangeCode(@PathVariable String code){
        userCouponService.exchangeCode(code);
    }

    @ApiOperation("查询我的优惠券")
    @GetMapping("page")
    public PageDTO<CouponVO> queryMyCoupon(UserCouponQuery query){
        return userCouponService.queryMyCoupon(query);
    }

    /**
     * 该方法是给tj-trade服务远程调用使用的
     * @param courses 订单中的课程信息
     * @return
     */
    @ApiOperation("查询可用优惠券方案")
    @PostMapping("available")
    public List<CouponDiscountDTO> findDiscountSolution(@RequestBody List<OrderCourseDTO> courses){
        return userCouponService.findDiscountSolution(courses);
    }

    @ApiOperation("根据券方案计算订单优惠明细")
    @PostMapping("discount")
    public CouponDiscountDTO queryDiscountDetailByOrder(
            @RequestBody OrderCouponDTO orderCouponDTO
    ){
        return userCouponService.queryDiscountDetailByOrder(orderCouponDTO);
    }

    @ApiOperation("核销优惠券")
    @PutMapping("use")
    public void writeOffCoupon(@ApiParam("用户优惠券集合") @RequestParam("couponIds") List<Long> userCouponIds){
        userCouponService.writeOffCoupon(userCouponIds);
    }

    @ApiOperation("退还优惠券")
    @PutMapping("refund")
    public void refundCoupon(@ApiParam("用户优惠券id集合") @RequestParam("couponIds") List<Long> userCouponIds){
        userCouponService.refundCoupon(userCouponIds);
    }

    @ApiOperation("分页查询我的优惠券接口")
    @GetMapping("rules")
    public List<String> queryDiscountRules(
            @ApiParam("用户优惠券id集合") @RequestParam("couponIds") List<Long> userCouponIds){
        return userCouponService.queryDiscountRules(userCouponIds);
    }

}
